package com.wing.productionPlan.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.productionPlan.model.entity.ProQualityTask;
import com.wing.productionPlan.model.entity.ProQualityWay;
import com.wing.productionPlan.model.request.ProQualityTaskUpdateForm;
import com.wing.productionPlan.model.request.ProQualityWayAddForm;
import com.wing.productionPlan.model.response.ProQualityWayVo;
import com.wing.productionPlan.service.ProQualityWayService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Validated
@Api(value = "质检方法", tags = "#/product/qualityTask/qualityWay")
@RestController("ProductionQualityWayController")
@RequestMapping("quality/admin/qualityWay")
@RequiredArgsConstructor
public class ProductionQualityWayController {

    private final ProQualityWayService proQualityWayService;;

    @ApiOperation(value = "查询质检方法列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProQualityWayVo> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            , String qualityWayName
            , String qualityCode

    ){
        Page<ProQualityWayVo> pg = new Page<>(page, limit);
        Map map = new HashMap();
        map.put("qualityWayName", qualityWayName);
        map.put("qualityCode", qualityCode);
        Page<ProQualityWayVo> list = proQualityWayService.pageList4app(pg, map);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@RequestBody ProQualityWayAddForm proQualityWayAddForm) {
         return proQualityWayService.add(proQualityWayAddForm);
    }

    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping("/update")
    public JsonResult update(@RequestBody ProQualityWayAddForm proQualityTaskUpdateForm) {
        ProQualityWay proQualityWay = proQualityWayService.getById(proQualityTaskUpdateForm.getId());
        if(proQualityWay == null){
            return JsonResult.fail("修改失败！");
        }
        if(!proQualityTaskUpdateForm.getQualityCode().equals(proQualityWay.getQualityCode())){
            //校验唯一性
            List<ProQualityWayVo> proQualityWayVoByCode = proQualityWayService.getProQualityWayVoByCode(proQualityTaskUpdateForm.getQualityCode());
            if(proQualityWayVoByCode.size()>0){
                return JsonResult.fail("当前方法代码已经存在！");
            }
        }

        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProQualityWay.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(proQualityTaskUpdateForm, proQualityWay, copyOptions);
        proQualityWay.setUpdateTime(LocalDateTime.now());
        proQualityWay.setModifier(JwtUtils.getLoginUserId());
        proQualityWay.setModifierName(JwtUtils.getLoginUsername());
        if (proQualityWayService.updateById(proQualityWay)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    @ApiOperation(value = "获取方法")
    @GetMapping("/get")
    public JsonResult get(Long id) {
        return JsonResult.success(proQualityWayService.getById(id));
    }


}
